package com.llxz.blog.article.service.impl;

import com.llxz.blog.article.dto.CollectParam;
import com.llxz.blog.article.entity.Article;
import com.llxz.blog.article.repository.ArticleRepository;

import com.llxz.blog.article.repository.CollectionRepository;
import com.llxz.blog.article.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import com.llxz.blog.article.entity.Collection;
@Service(value = "CollectService")
@Component
public class CollectServiceImpl implements CollectService {
    @Autowired
    private CollectionRepository collectionRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ArticleRepository articleRepository;

    @Override
    public void collectArticle(CollectParam collectParam) {
        Collection collection = collectionRepository.findByPhone(collectParam.getPhone());
        if (collection != null) {
            Query query = Query.query(Criteria.where("phone").is(collectParam.getPhone()));
            Update update = new Update();
            update.addToSet("collection", collectParam.getArticleId());
            mongoTemplate.upsert(query, update, Collection.class, "collection");
        } else {
            Collection collection1 = new Collection();
            collection1.getCollection().add(collectParam.getArticleId());
            collection1.setPhone(collectParam.getPhone());
            mongoTemplate.insert(collection1, "collection");
        }
    }

    @Override
    public List<Article> getOnesCollection(String phone) {
        List<Article> res = new ArrayList<>();
        Collection collection = collectionRepository.findByPhone(phone);
        if (collection == null ) {
            return null;
        } else {
            List<String> list = collection.getCollection();
            if (list != null) {
                for (String str : list) {
                    if (str != null) {
                        res.add(articleRepository.findByArticleId(str));
                    }
                }
                return res;
            } else {
                return null;
            }
        }
    }

    @Override
    public void cancelCollection(CollectParam collectParam) {
        Collection collection = collectionRepository.findByPhone(collectParam.getPhone());
        collectionRepository.removeByPhone(collectParam.getPhone());
        collection.getCollection().remove(collectParam.getArticleId());
        mongoTemplate.insert(collection, "collection");
    }

    @Override
    public boolean ifCollect(CollectParam collectParam) {
        String articleId = collectParam.getArticleId();
        String phone = collectParam.getPhone();
        Collection collection = collectionRepository.findByPhone(phone);
        if (collection == null) {
            return false;
        } else {
            List<String> list = collection.getCollection();
            for (String str : list) {
                if (str.equals(articleId)) {
                    return true;
                }
            }
        }
        return false;
    }
}

